<?php
namespace app\index\controller;
use think\Db;
use think\Session;
use think\Controller;
use think\Request;
class Login extends Controller
{
    public function index()
    {
            $website=website();//获取基本资料
            header("content-type:text/html;charset=utf-8");
            $code = input('code');

            if ($code){//判断code是否存在
                $userinfo = getUserInfo($code);

                //判断code是否能获取，否则重新获取
                if(!$userinfo['openid']){
                    $Wechat_url="https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$website['appid']."&redirect_uri=".$website['web_url']."index.php/index/login/index&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect";
                    $this->redirect($Wechat_url);
                }
            
                //用openid 获取信息
                $where['openid']=$userinfo['openid'];
                $user=Db::name('user')->where($where)->find();

                if($user){//存在此用户则继续
                    Session::set('wechat_user',$user['openid']);
                    if($user['telphone']){//已填写手机号则跳转到首页
                        $this->redirect('index/index');
                    }else{//没有填写手机则要求填写，短信认证
                        $this->view->assign('website', $website);
                        $this->view->assign('Assets', Assets);
                        return $this->view->fetch();
                    }
                }else{//不存在此用户则注册
                    $data['addtime']=date('Y-m-d H:i:s',time());
                    $data['openid']=$userinfo['openid'];
                    $data['sex']=$userinfo['sex'];
                    $data['area']=$userinfo['country']."_".$userinfo['province']."_".$userinfo['city'];
                    $data['head']=$userinfo['headimgurl'];
                    $data['username']=base64_encode($userinfo['nickname']);
                    if(Db::name('user') -> insert($data)){
                        $this->redirect('login/index');
                    }else{
                        echo "注册异常，请刷新重新提交。";
                    }
                }
            }else{
                //不存在CODE 则重新获取
                $Wechat_url="https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$website['appid']."&redirect_uri=".$website['web_url']."index.php/index/login/index&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect";
                $this->redirect($Wechat_url);
            }
    }

    /*
    发送验证码
     */
    public function sendverify()
    {
        $phone=input('data');
        $msg=sendsns($phone);
        if($msg=='SUCCESS'){
            return '发送成功';
        }else{
            return $msg;
        }
    }


    /*
    验证 验证码
     */
    public function checkverify()
    {
        $data=input('data');
        $data=json_decode($data,ture);
        $verify=Session::get('msn');
        $telphone=Session::get('telphone');
        if($data['verify']==$verify){
            if($telphone!=$data['phone']){
                return $telphone;
            }
            $user=Db::name('user');
            $check_data['telphone']=$data['phone'];
            $check_phone=$user->where($check_data)->find();
            if($check_phone){
                return '此手机号已绑定其他微信！';
            }
            $where['openid']=Session::get('wechat_user');
            $save['telphone']=$data['phone'];
            $user->where($where)->update($save);
            $msg='绑定成功';
        }else{
            $msg='验证码错误，请重新获取！';
        }
        return $msg;
    }


}
